package com.ruoyi.web.controller.lh;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.tool.DeduplicationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lh.domain.Machine;
import com.ruoyi.lh.service.IMachineService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 设备Controller
 *
 * @author LH
 * @date 2023-08-01
 */
@Controller
@RequestMapping("/lh/machine")
public class MachineController extends BaseController
{
    private String prefix = "lh/machine";

    @Autowired
    private IMachineService machineService;

    @RequiresPermissions("lh:machine:view")
    @GetMapping()
    public String machine(Model model)
    {
    model.addAttribute(
        "machineList",
        machineService.selectMachineList(new Machine()).stream()
            .filter(DeduplicationUtil.distinctByKey(Machine::getMachineNo)).collect(Collectors.toList()));
        return prefix + "/machine";
    }

    /**
     * 查询设备列表
     */
    @RequiresPermissions("lh:machine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Machine machine)
    {
        startPage();
        List<Machine> list = machineService.selectMachineList(machine);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @RequiresPermissions("lh:machine:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Machine machine)
    {
        List<Machine> list = machineService.selectMachineList(machine);
        ExcelUtil<Machine> util = new ExcelUtil<Machine>(Machine.class);
        return util.exportExcel(list, "设备数据");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("projectList", machineService.findProjectList());
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @RequiresPermissions("lh:machine:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Machine machine)
    {
        return toAjax(machineService.insertMachine(machine));
    }

    /**
     * 修改设备
     */
    @RequiresPermissions("lh:machine:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Machine machine = machineService.selectMachineById(id);
        mmap.put("machine", machine);
        mmap.addAttribute("projectList", machineService.findProjectList());
        return prefix + "/edit";
    }

    /**
     * 修改保存设备
     */
    @RequiresPermissions("lh:machine:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Machine machine)
    {
        return toAjax(machineService.updateMachine(machine));
    }

    /**
     * 删除设备
     */
    @RequiresPermissions("lh:machine:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(machineService.deleteMachineByIds(ids));
    }

    @RequiresPermissions("lh:machine:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Machine> util = new ExcelUtil<Machine>(Machine.class);
        util.importTemplateExcel(response, "设备数据");
    }

    @Log(title = "设备", businessType = BusinessType.IMPORT)
    @RequiresPermissions("lh:machine:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Machine> util = new ExcelUtil<Machine>(Machine.class);
        List<Machine> machineList = util.importExcel(file.getInputStream());
        String message = machineService.importMachine(machineList, false, getLoginName());
        return AjaxResult.success(message);
    }
}
